﻿#include "admsglist.h"
#pragma execution_character_set("utf-8")
AdMsgList::AdMsgList(QListWidget *_mlistWidget, QObject *parent):
    QObject(parent)
{
    mlistWidget=_mlistWidget;
    //不显示行向滚动条，子项文本过长自动显示...
    mlistWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mlistWidget->setContextMenuPolicy(Qt::ActionsContextMenu);//设置右键菜单
    QAction *copyAction = new QAction("Copy", mlistWidget);
    copyAction->setShortcut(QKeySequence::Copy); //设置快捷键
    mlistWidget->addAction(copyAction);
    //连接action的triggered信号和槽函数
    connect(copyAction, SIGNAL(triggered()), this, SLOT(onCopyTriggered()));
}

AdMsgList::~AdMsgList()
{

}
// 触发Copy
void AdMsgList::onCopyTriggered()
{
    //获取当前action
    QAction *action = qobject_cast<QAction*>(sender());
    if (action&&mlistWidget->currentItem())
    {
        //获取要复制的内容
        QString content = mlistWidget->currentItem()->toolTip();
        qDebug()<<mlistWidget->currentRow()<<"行，获取要复制的内容:"<<content;
        //将内容复制到剪贴板
        QClipboard *clipboard = QApplication::clipboard();
        clipboard->setText(content);
    }
}
